package com.lzit.smart_canteen.service;

import com.lzit.smart_canteen.domain.CartItem;
import com.lzit.smart_canteen.domain.Order;
import com.lzit.smart_canteen.domain.OrderItem;
import com.lzit.smart_canteen.repository.CartItemRepository;
import com.lzit.smart_canteen.repository.OrderItemRepository;
import com.lzit.smart_canteen.repository.OrderRepository;
import jakarta.persistence.criteria.CriteriaBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemService {
    @Autowired
    private CartItemRepository cartItemRepository;
    @Autowired
    private OrderItemRepository orderItemRepository;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    //创建购物车Item
    public void addItem(CartItem item) {
        cartItemRepository.save(item);
    }

    //创建订单Item
    public void addItem(OrderItem orderItem) {
        orderItemRepository.save(orderItem);
    }

    //通过Uid查找购物车所有Item
//    @Caching(cacheable = {@Cacheable(cacheNames = "CartItems", key = "#uid")})
//    @Cacheable(cacheNames = "CartItems", key = "#uid")
    public List<CartItem> queryCart(Integer uid) {
        return cartItemRepository.findCartItemsByUid(uid);
    }

    //通过Fid查找购物车单个Item
    public CartItem findCartItemByFid(Integer fid) {
        return cartItemRepository.findCartItemByFid(fid);
    }

    //通过Fid和Uid联合查找单个Item
    public CartItem findCartByUidAndFid(Integer uid,Integer fid) {
        return cartItemRepository.findCartItemByUidAndFid(uid,fid);
    }

    //更新购物车单个Item数量
//    @CacheEvict(cacheNames = "CartItems", key = "#uid", beforeInvocation = true)
    public void updateCartItem(Integer uid,Integer fid, Integer count) {
        cartItemRepository.updateCountByUidAndFid(uid, fid, count);
    }

    //通过Uid删除购物车所有Item
    public void deleteCartItemByUid(Integer uid) {
        cartItemRepository.deleteCartItemByUid(uid);
    }

    //通过Uid和Fid删除购物车单个Item
    public void deleteCartItemByUidAndFid(Integer uid, Integer fid) {
        cartItemRepository.deleteCartItemByUidAndFid(uid,fid);
    }

    //通过Oid删除订单所有Item
    public void deleteOrderItemsByOid(String oid) {
        orderItemRepository.deleteOrderItemsByOid(oid);
    }

    //通过Oid查找订单所有Item
    public List<OrderItem> findOrderItemsByOid(String oid){
        return orderItemRepository.findOrderItemsByOid(oid);
    }

    //通过Oid更新订单状态信息
    public void updateOrderItem(String status,String oid){
        orderItemRepository.updateStatusByOid(status,oid);
    }
    //通过window和status查找订单item
    public List<OrderItem> findOrderItemsByWindowAndStatus(Integer window,String status){
        return orderItemRepository.findOrderItemsByWindowAndStatus(window,status);
    }
    //通过oid和status查找订单items
    public List<OrderItem> findOrderItemsByOidAndStatus(String oid,String status) {
        return orderItemRepository.findOrderItemsByOidAndStatus(oid,status);
    }
    //通过oid和fid更新订单item信息
    public void updateItemStatusByOidAndFid(String status,String oid,Integer fid){
        orderItemRepository.updateItemStatusByOidAndFid(status, oid, fid);
    }
}
